package com.morehair.service.wx.impl;

import com.github.pagehelper.PageHelper;
import com.morehair.bean.bo.wx.CollectionStatusBo;
import com.morehair.bean.pojo.Collect;
import com.morehair.bean.pojo.Goods;
import com.morehair.bean.pojo.User;
import com.morehair.bean.vo.wx.CollectListDataVo;
import com.morehair.bean.vo.wx.CollectionOperationVo;
import com.morehair.mapper.CollectMapper;
import com.morehair.mapper.GoodsMapper;
import com.morehair.service.wx.WxCollectService;
import com.morehair.utils.GetNowUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Date: 2021/6/9
 * Time: 23:15
 * author: zhang wei
 */
@Service
@Transactional
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CollectMapper collectMapper;


    @Override
    public CollectListDataVo getCollectList(Integer type, Integer page, Integer size) {
        User user = GetNowUser.getUser();
        Integer userId = user.getId();
        //根据当前用户Id和收藏类型type为已收藏：type=0 来查找collect表中的数据
        List<Collect> collects = collectMapper.selectByTypeAndUserId(userId,type);
        int total = collects.size();
        double v = (double) total / size;
        //向上取整
        page = (int) Math.ceil(v);
        PageHelper.startPage(page, size);

        List<CollectListDataVo.CollectList> collectLists = new ArrayList<>();
        for (Collect collect : collects) {
            Integer goodsId = collect.getValueId();

            CollectListDataVo.CollectList collectList = new CollectListDataVo.CollectList();
            collectList.setId(collect.getId());
            collectList.setValueId(goodsId);
            collectList.setType(collect.getType());

            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            collectList.setName(goods.getName());
            collectList.setBrief(goods.getBrief());
            collectList.setPicUrl(goods.getPicUrl());
            collectList.setRetailPrice(goods.getRetailPrice());

            collectLists.add(collectList);
        }
        CollectListDataVo collectListDataVo = new CollectListDataVo(page,collectLists);
        return collectListDataVo;
    }

    @Override
    public CollectionOperationVo addOrDeleterCollect(CollectionStatusBo collectionStatusBo) {



        return null;
    }
}
